package com.yht.springboot_yht.controller;

import com.yht.springboot_yht.pojo.ResponseMessage;
import com.yht.springboot_yht.pojo.User;
import com.yht.springboot_yht.pojo.dto.UserDto;
import com.yht.springboot_yht.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController //return object, convert to json
@RequestMapping("/user") //localhost:8088/user/**
public class UserController {

    @Autowired
    IUserService userService;




    //REST
    //add
    @PostMapping
    public ResponseMessage<Object> add(@Validated @RequestBody UserDto user) {
        User userNew = userService.add(user);
        return ResponseMessage.success(userNew);
    }

    //query
    @GetMapping("{userId}") //URL：localhost:8088/user/1
    public ResponseMessage<Object> findById(@PathVariable Integer userId) {
        User userNew = userService.getUser(userId);
        return ResponseMessage.success(userNew);
    }


    //modify
    @PutMapping
    public ResponseMessage<Object> edit(@Validated @RequestBody UserDto user) {
        User userNew = userService.edit(user);
        return ResponseMessage.success(userNew);
    }

    //remove
    @DeleteMapping({"{userId}"})
    public ResponseMessage<Object> delete(@PathVariable Integer userId) {
        userService.delete(userId);
        return ResponseMessage.success();
    }
}
